
# -*- coding: utf-8 -*-
# @Author: Gemini by Tim Liu 
# @Date: 2024-07-15
# @Last Modified by: Tim Liu
# @Last Modified time: 2024-07-15

# @Description: Document Intelligence Service

from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient

from azure.ai.documentintelligence.models import AnalyzeDocumentRequest

#from core.exception import CustomException
from config.settings import *

class DocintelService(object):
    """
    Document Intelligence Service, used to analyze documents and extract information via azure document intelligence.
    """

    def __init__(self, model: str = None):
        self.model = model
    
    def analyze_document(self, url):
        document_intelligence_client = DocumentIntelligenceClient(
            endpoint=DOCINTEL_AZURE_OPENAI_ENDPOINT, credential=AzureKeyCredential(DOCINTEL_AZURE_OPENAI_API_KEY)
        )
        
        if self.model is None:
            model = "prebuilt-read"
        else:
            model = self.model
        
        #transform speech to text         
        poller = document_intelligence_client.begin_analyze_document(
            model, AnalyzeDocumentRequest (url_source = url
        ))
        
        result = poller.result()
        
        return result